package com.henry.librarymng;

import java.util.ArrayList;
import java.util.Date;

public class BookEntry {
    String title;
    int bookID;
    int copiesNum;
    ArrayList<BorrowingInfo> borrowings;

    public BookEntry(String name, int id) {
        this.title = name;
        this.bookID = id;
        this.borrowings = new ArrayList<BorrowingInfo>();
        copiesNum = 1;
    }

    public void returning(String readerID) throws Exception {
        if (borrowings.removeIf(borrowing -> borrowing.readerID.equals(readerID))) {
            copiesNum++;
        }
        else {
            throw new Exception("The reader didn't borrow this book.");
        }
    }

    public void borrowing(String readerID, Date borrowDate) throws Exception {
        if (copiesNum < 1) throw new Exception("No available copy.");
        else {
            borrowings.add(new BorrowingInfo(readerID, borrowDate));
            copiesNum--;
        }
    }

    public String getBookTitle() {
        return title;
    }

    public boolean available() { return copiesNum > 0; }

    public void addCopies() { copiesNum++; }

    @Override
    public String toString() {
        return "'" + title + "' Available Copies: " + copiesNum;
    }
}
